﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Routing;
using VelocityShop;
using VelocityShop.Entities;

namespace VelocityShop.Web.UI
{
    public partial class BrowseCatalog : System.Web.UI.Page
    {
        private CatalogView _catalog;

        public Catalog Catalog
        {
            get
            {
                if (_catalog == null)
                {
                    Catalog tmpCat = CatalogManager.GetCatalog();
                    _catalog = new CatalogView(tmpCat.Id,tmpCat.Currency);
                    _catalog.CurrentCategory = this.CurrentCategory;
                    _catalog.CurrentTag = this.CurrentTag == null ? string.Empty : this.CurrentTag;
                    _catalog.Categories = tmpCat.Categories;
                    _catalog.LastUpdate = tmpCat.LastUpdate;
                    
                }
                
                return _catalog; 
            }
        }

        public int CurrentPageNumber
        {
            get {

                return Convert.ToInt32(Request.QueryString["page"]);
            }
        }

        public string CurrentCategory
        {
            get
            {
                return RouteData.Values["category"] == null ? "Televisions": RouteData.Values["category"] as string;
            }
        }

        public string CurrentTag
        {
            get
            {
                return Request.QueryString["tag"];
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            CatalogViewer1.OnPageIndexChanging += CatalogViewer1_OnPageIndexChanging;
            CatalogViewer1.OnCartItemAdding += CatalogViewer1_OnCartItemAdding;
            ((DefaultMaster)this.Master).SetCurrentCategory(CurrentCategory);

            foreach (Category cat in Catalog.Categories)
            {
                if (cat.Name.ToLower() == CurrentCategory.ToLower())
                {
                    CatalogViewer1.AvailableTags = cat.AvailableTags;
                    break;
                }
            }

            CatalogViewer1_OnPageIndexChanging(this, new CatalogViewer.CatalogEventArgs(CurrentPageNumber,CurrentCategory,CurrentTag));                              
            UpdateCart();

        }

        private void UpdateCart()
        {
            ShoppingCart1.UpdateCart();
        }

        protected void CatalogViewer1_OnPageIndexChanging(object sender, CatalogViewer.CatalogEventArgs e)
        {
            BindCatalogItems();
        }

        protected void CatalogViewer1_OnCartItemAdding(object sender, CatalogViewer.CartItemEventArgs e)
        {
            UserSession.ShoppingCart.Add(Catalog.Items.First(item => item.ProductId == e.ProductId));
            UpdateCart();
            BindCatalogItems();
        }

        private void BindCatalogItems()
        {
            CatalogViewer1.CurrentCategory = CurrentCategory;
            CatalogViewer1.CurrentTag = CurrentTag;
            CatalogViewer1.CurrentPageNumber = CurrentPageNumber;
            CatalogViewer1.TotalItemsCount = Catalog.Items.Count;          
            CatalogViewer1.PageSize = 20;
            CatalogViewer1.BasePageHREF = string.Format("BrowseCatalog.aspx");
            CatalogViewer1.DataSource = (from p in Catalog.Items orderby p.ProductId select p).Skip((CatalogViewer1.CurrentPageNumber-1) * CatalogViewer1.PageSize).Take(CatalogViewer1.PageSize);
            CatalogViewer1.LastUpdate = Catalog.LastUpdate;
        }
       
    }
}
